package com.vinhvt.effiport.data;

import java.io.File;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.vinhvt.effiport.financial.ep.Organization;
import com.vinhvt.effiport.financial.ep.Transaction;

public class DataManager {
	private DataProvider provider;
	
	public DataManager() throws SQLException{	
		
		/*	Oracle Sample
		 * String serverName = "127.0.0.1";
		 * String portNumber = "1521";
		 * String sid = "xe";
		 * String connectionURL = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
		 * String user = "sasi";
		 * String password = "sasi";
		 * 		
		 * */
		
		///*	MySQl sample data
		ConnectionManager manager;
		try {				
		String database = "effiport";
		String user = "root";
		String password = "";
		manager = new ConnectionManagerMySQL(database, user, password);
		this.provider = new DefaultDataProvider(manager);		
		} catch (Exception e) {
			e.printStackTrace();
		}
		//*/			
	}
		
	
	public DataManager(File databaseFolder) {
		boolean creation = !databaseFolder.exists();
		String connectionURL = "jdbc:derby:" + databaseFolder.getAbsolutePath() + (creation ? ";create=true;" : ";");
		String username = "";
		String password = "";
		String scriptResource = creation ? "dinoage_derby.sql" : null;
		ConnectionManager manager = new ConnectionManagerDerby(connectionURL, username, password, scriptResource);
		provider = new DefaultDataProvider(manager);
	}
	
	public DataManager(DataProvider provider){
		this.provider = provider;
	}
		
	public void createOrganization(Organization org) {
		try {
			provider.createOrganization(org);			
			
		} catch (Exception e) {			
			e.printStackTrace();
		}		
	}
	
	public void createTransaction(Transaction tran) {
		try {
			provider.createTransaction(tran);			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public List<Transaction> getTransaction(String symbol) {
		return provider.getTransaction(symbol);
	}	
	
	/**
	 * Gets the transaction.
	 * 
	 * @param symbol the symbol
	 * @param fromDate the from date
	 * @param toDate the to date
	 * 
	 * @return the transaction
	 */
	public List<Transaction> getTransaction(String symbol, Date fromDate, Date toDate) {
		return provider.getTransaction(symbol, fromDate, toDate);
	}
	
	/**
	 * Gets the stock list.
	 * 
	 * @return the stock list
	 */
	public List<String> getStockList() {
		return provider.getStockList();
	}
	
	public List<Double> getClosedPrice(String symbol) {
		return provider.getClosedPrice(symbol);
	}
}
